package com.example.xui.Main;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xui.Classification.classification_computer;
import com.example.xui.Classification.classification_medicine;
import com.example.xui.Classification.classification_nursing;
import com.example.xui.Classification.classification_pharmacy;
import com.example.xui.R;
import com.example.xui.Shezhi;
import com.google.android.material.tabs.TabLayout;
import com.xuexiang.xui.widget.tabbar.EasyIndicator;
import com.yalantis.phoenix.PullToRefreshView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class SecondFragment extends Fragment {
    String[] pages={"计算机","药学","护理学","医学","考研","文管"};
    static int i=0;
    private List<Fragment> list;
    private TabLayout tabLayout;
    private FragmentManager manager;
    private PullToRefreshView mPullToRefreshView;
    private ViewPager viewPager;
    String string;
    List<View> views=new ArrayList<>(6);
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
    }
    private MyAdapter myAdapter;
    RecyclerView mRecyclerView;
    List<JSONObject> list1=new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
      //  second_data("计算机");
       View view=null;
       view=getLayoutInflater().inflate(R.layout.fragment_classification_pharmacy,null);
       views.add(view);
       view=getLayoutInflater().inflate(R.layout.fragment_classification_pharmacy,null);
       views.add(view);
       view=getLayoutInflater().inflate(R.layout.fragment_classification_pharmacy,null);
       views.add(view);
       view=getLayoutInflater().inflate(R.layout.fragment_classification_pharmacy,null);
       views.add(view);
       view=getLayoutInflater().inflate(R.layout.fragment_classification_pharmacy,null);
        views.add(view);
        view=getLayoutInflater().inflate(R.layout.fragment_classification_pharmacy,null);
        views.add(view);
        init();
       // string="医学";
        second_data("计算机");
        initEvn();
    }
    private  void initEvn(){
        tabLayout.addOnTabSelectedListener(listener);
    }
    private void init(){
        tabLayout=getActivity().findViewById(R.id.tabSegment);
        viewPager=getActivity().findViewById(R.id.view_pager1);
        tabLayout.setupWithViewPager(viewPager,false);
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return pages.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = views.get(position);
                mRecyclerView = view.findViewById(R.id.telephone);
                mPullToRefreshView=view.findViewById(R.id.pull_to_refresh);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                    return pages[position];
            }
        });
    }

    private TabLayout.OnTabSelectedListener listener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            //选择的tab
            Log.e("TT","onTabSelected:" + tab.getText().toString());
            string=tab.getText().toString();
            second_data(string);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            //离开的那个tab
         /*   string=tab.getText().toString();
            second_data(string);*/
            Log.e("TT","onTabUnselected" + tab.getText().toString());
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            //再次选择tab
            string=tab.getText().toString();
            second_data(string);
            Log.e("TT","onTabReselected" + tab.getText().toString());
        }
    };

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    public static SecondFragment newInstance() {
        SecondFragment fragment1 = new SecondFragment();
        return fragment1;
    }
    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {
        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(getContext(), R.layout.adapter_note2, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
            Log.i("tag","tag1="+list1);
            try {
                if(!list1.equals("[]")) {
                    Log.i("tag","tag=hello");
                    holder.pictureto.setImageBitmap(BitmapFactory.decodeFile(list1.get(position).getString("resourcePicture")));
             //       holder.mTitleContent.setText(list1.get(position).getString("resourceContent"));
                    holder.mTitleid.setText(list1.get(position).getString("resourceNickname"));
                    holder.mTitledTitle.setText(list1.get(position).getString("resourceTitle"));
                    holder.classification.setText(list1.get(position).getString("resourceType"));
                    holder.likenum.setText(list1.get(position).getString("resourceThumbNum"));
                    holder.commentnum.setText(list1.get(position).getString("resourceConmentNum"));
                }
             /*   holder.mTitleContent.setText(list1.get(position).getString("resourceContent"));
                holder.mTitleid.setText(list1.get(position).getString("resourceNickname"));
                holder.mTitledTitle.setText(list1.get(position).getString("resourceTitle"));
                holder.classification.setText(list1.get(position).getString("resourceType"));
                holder.likenum.setText(list1.get(position).getString("resourceThumbNum"));
                holder.commentnum.setText(list1.get(position).getString("resourceConmentNum"));*/
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public int getItemCount() {
            int a=0;
            //return list1.size();
            if(!list1.equals("[]")){
                a=list1.size();
            }
            return a;
        }
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView mTitleid;
        //TextView mTitleContent;
        ImageView pictureto;
        TextView mTitledTitle;
        TextView classification;
        TextView likenum;
        TextView commentnum;
        TextView data;

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            pictureto = itemView.findViewById(R.id.imageView17);
            mTitleid = itemView.findViewById(R.id.deviceName);
         //   mTitleContent = itemView.findViewById(R.id.deviceAddress);
            mTitledTitle = itemView.findViewById(R.id.textView9);
            classification = itemView.findViewById(R.id.textView47);
            likenum = itemView.findViewById(R.id.textView51);
            commentnum = itemView.findViewById(R.id.textView53);
            data = itemView.findViewById(R.id.textView54);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
      //  second_data("医学");
    }

    public void second_data(String string){
        new Thread(){
            @Override
            public void run() {
                super.run();
                RequestParams params = new RequestParams("http://10.138.87.49:8080/resource/resoucrceselectType");
                params.addHeader("Content-Type","application/x-www-form-urlencoded");
                params.addBodyParameter("resourceType",string);
                Log.i("tag","种类="+string);
                x.http().post(params, new Callback.CacheCallback<JSONArray>() {
                    @Override
                    public void onSuccess(JSONArray result) {
                        List<JSONObject> list = new ArrayList<>();
                        for (int i = 0; i < result.length(); i++) {
                            try {
                                JSONObject jsonObject = result.getJSONObject(i);
                                list.add(jsonObject);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                list1=list;
                                Log.i("tag","种类="+list1);
                                myAdapter = new MyAdapter();
                                mRecyclerView.setAdapter(myAdapter);
                                LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                                mRecyclerView.setLayoutManager(layoutManager);
                                mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
                                    @Override
                                    public void onRefresh() {
                                        mPullToRefreshView.postDelayed(new Runnable() {
                                            @Override
                                            public void run() {
                                                /*list1=list;
                                                Log.i("tag","种类="+list1);
                                                myAdapter = new MyAdapter();
                                                mRecyclerView.setAdapter(myAdapter);
                                                LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                                                mRecyclerView.setLayoutManager(layoutManager);*/
                                                mPullToRefreshView.setRefreshing(true);
                                            }
                                        }, 2);
                                    }
                                });
                                //    String son=getActivity().getIntent().getStringExtra("sno");
                                  /*  try {
                                        Log.i("Tag","tt="+list1.get(0).getString("resourceID"));
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }*/
// refresh ui 的操作代码             list1.a
                            }

                        });

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public boolean onCache(JSONArray result) {
                        return false;
                    }
                });
            }
        }.start();
    }



}
